package com.sapitest.imp;

import com.sapitest.imp.dao.GetGoodsList;
import com.sapitest.imp.dao.MyShoppingTrolley;
import com.sapitest.imp.model.DeviceInfo;
import com.sapitest.imp.model.DevicesID;
import com.sapitest.imp.model.ExpectedID;
import com.sapitest.utils.ConfigFile;
import com.sapitest.utils.MyBatisUtil;

/**
 * 从数据库获取测试数据
 */
public class TestData {
    private static final String GET_GOODS_LIST_TABLE = ConfigFile.getTestParameter("GET_GOODS_LIST_TABLE");
    private static final String MY_SHOPPING_TROLLEY_TABLE = ConfigFile.getTestParameter("MY_SHOPPING_TROLLEY_TABLE");

    //mybatis mapper文件SQL的ID
    private static final String GET_GOODS_LIST_ID = "GetGoodsList";
    private static final String MY_SHOPPING_TROLLEY_ID = "MyShoppingTrolley";
    private static final String GET_DEVICES_INFO_ID = "GetDevicesInfo";
    private static final String DEVICE_INFO_ID = "DeviceInfo";
    private static final String EXPECTED_ID = "Expected";

    /**
     * 获取接口请求数据
     * @param caseID 测试用例编号
     * @return  json String
     */
    public static String getGoodsListRequestData(int caseID){
        GetGoodsList ggl = getTestDataFromDB(GET_GOODS_LIST_ID, caseID);
        ggl.setDevicesInfo(getDeviceInfo(GET_GOODS_LIST_TABLE, caseID));
        return ggl.toJSONString();
    }

    /**
     * 获取接口请求数据
     * @param caseID 测试用例编号
     * @return  json String
     */
    public static String myShoppingTrolleyRequestData(int caseID){
        MyShoppingTrolley mst = getTestDataFromDB(MY_SHOPPING_TROLLEY_ID, caseID);
        mst.setDevicesInfo(getDeviceInfo(MY_SHOPPING_TROLLEY_TABLE, caseID));
        return mst.toJSONString();
    }

    /**
     * 从某接口测试数据表中获取期望结果
     * @param interfaceTable    某接口测试数据对应的表名
     * @param caseID    测试用例编号
     * @return  期望结果字符串
     */
    public static String getExpResponseData(String interfaceTable, int caseID){
        ExpectedID expectedID = new ExpectedID();
        expectedID.setTable_name(interfaceTable);
        expectedID.setId(caseID);
        return getExpResData(expectedID);
    }

    /**
     * 获取期望结果
     * @param expectedID    ExpectedID JavaBean
     * @return  期望结果字符串
     */
    private static String getExpResData(ExpectedID expectedID){
        return MyBatisUtil.newInstance().selectOne(EXPECTED_ID, expectedID);
    }

    /**
     * 获取设备表对应的主键id
     * @param devicesID DevicesID JavaBean
     * @return  设备表对应的主键id
     */
    private static int getDeviceInfoID(DevicesID devicesID){
        return MyBatisUtil.newInstance().selectOne(GET_DEVICES_INFO_ID, devicesID);
    }

    /**
     * 从设备表获取设备参数信息
     * @param interfaceTable    某接口测试数据对应的表名
     * @param caseID    测试用例编号
     * @return  设备信息 DeviceInfo
     */
    private static DeviceInfo getDeviceInfo(String interfaceTable, int caseID){
        DevicesID devicesID = new DevicesID();
        devicesID.setTable_name(interfaceTable);
        devicesID.setId(caseID);
        return getTestDataFromDB(DEVICE_INFO_ID, getDeviceInfoID(devicesID));
    }

    /**
     * 从数据库查询数据
     * @param mapperId  mapper 文件SQL的ID
     * @param caseID    数据库某表主键ID值
     * @param <T>   泛型
     * @return  泛型
     */
    private static <T> T getTestDataFromDB(String mapperId, int caseID){
        MyBatisUtil myBatisUtil = MyBatisUtil.newInstance();
        return myBatisUtil.selectOne(mapperId, caseID);
    }

}
